package mobile

import (
	"0306zk/common/initialize"
	"fmt"
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Mobile   string `gorm:"type:char(11);not null;index:ip_idx commit:'电话号'"`
	Password string `gorm:"type:varchar(32);not null;index:ip_idx commit:'密码'"`
}

func (u User) GetUserMobile(mobile string) (user *User, err error) {
	err = initialize.DB.Where("mobile=?", mobile).Take(&user).Error
	if err != nil {
		return nil, err
	}
	return
}

func (u User) CreateRegister(mobile string, password string) (user *User, err error) {
	user = &User{
		Mobile:   mobile,
		Password: password,
	}
	err = initialize.DB.Create(&user).Error
	if err != nil {
		return nil, err
	}
	return
}

func MysqlInit() {
	err := initialize.DB.AutoMigrate(&User{})
	if err != nil {
		fmt.Println("数据迁移失败")
		return
	} else {
		fmt.Println("数据迁移成功")
	}
}
